package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportGxlqTxhfjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportGxlqTxhfjlbBsInfoVo;
import com.ruoyi.report_lq.domain.ReportGxlqTxhfjlbBsInfo;
import com.ruoyi.report_lq.mapper.ReportGxlqTxhfjlbBsInfoMapper;
import com.ruoyi.report_lq.service.IReportGxlqTxhfjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 改性沥青弹性恢复、离析试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-03
 */
@RequiredArgsConstructor
@Service
public class ReportGxlqTxhfjlbBsInfoServiceImpl implements IReportGxlqTxhfjlbBsInfoService {

    private final ReportGxlqTxhfjlbBsInfoMapper baseMapper;

    private final ReportGxlqTxhfjlbTxBaDetailServiceImpl txService;

    private final ReportGxlqTxhfjlbRhdBaDetailServiceImpl rhdService;

    /**
     * 查询改性沥青弹性恢复、离析试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportGxlqTxhfjlbBsInfoVo queryById(Long excelId){
        ReportGxlqTxhfjlbBsInfoVo info = baseMapper.selectVoById(excelId);
        // TODO 弹性恢复率
        info.setRhdList(rhdService.queryByInfoId(excelId));
        // TODO 软化点差值
        info.setTxhfList(txService.queryByInfoId(excelId));
        return info;
    }

    /**
     * 查询改性沥青弹性恢复、离析试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportGxlqTxhfjlbBsInfoVo> queryPageList(ReportGxlqTxhfjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportGxlqTxhfjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportGxlqTxhfjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询改性沥青弹性恢复、离析试验检测记录列表
     */
    @Override
    public List<ReportGxlqTxhfjlbBsInfoVo> queryList(ReportGxlqTxhfjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportGxlqTxhfjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportGxlqTxhfjlbBsInfo> buildQueryWrapper(ReportGxlqTxhfjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportGxlqTxhfjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportGxlqTxhfjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportGxlqTxhfjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportGxlqTxhfjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportGxlqTxhfjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportGxlqTxhfjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportGxlqTxhfjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportGxlqTxhfjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportGxlqTxhfjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportGxlqTxhfjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportGxlqTxhfjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportGxlqTxhfjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportGxlqTxhfjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportGxlqTxhfjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportGxlqTxhfjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportGxlqTxhfjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportGxlqTxhfjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportGxlqTxhfjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportGxlqTxhfjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportGxlqTxhfjlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqcd()), ReportGxlqTxhfjlbBsInfo::getJizlqcd, bo.getJizlqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getGaixlqzl()), ReportGxlqTxhfjlbBsInfo::getGaixlqzl, bo.getGaixlqzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqbh()), ReportGxlqTxhfjlbBsInfo::getJizlqbh, bo.getJizlqbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGaixjmc()), ReportGxlqTxhfjlbBsInfo::getGaixjmc, bo.getGaixjmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqzl()), ReportGxlqTxhfjlbBsInfo::getJizlqzl, bo.getJizlqzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportGxlqTxhfjlbBsInfo::getJiancjl, bo.getJiancjl());
        return lqw;
    }

    /**
     * 新增改性沥青弹性恢复、离析试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportGxlqTxhfjlbBsInfoBo bo) {
        ReportGxlqTxhfjlbBsInfo add = BeanUtil.toBean(bo, ReportGxlqTxhfjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改改性沥青弹性恢复、离析试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportGxlqTxhfjlbBsInfoBo bo) {
        ReportGxlqTxhfjlbBsInfo update = BeanUtil.toBean(bo, ReportGxlqTxhfjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        // TODO 弹性恢复率
        txService.insertOrUpdateByTxDetail1Bo(bo.getTxhfList(),update.getExcelId());
        //TODO 软化点差值
        rhdService.insertOrUpdateByRhdDetail1Bo(bo.getRhdList(),update.getExcelId());
        return flag;
    }




    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportGxlqTxhfjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除改性沥青弹性恢复、离析试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
